package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;//引入套餐Mapper
    @Autowired
    private SetmealDishMapper setmealDishMapper;//引入套餐菜品Mapper
    @Autowired
    private RedisTemplate redisTemplate;//引入RedisTemplate

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页查询参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //调用Mapper方法进行分页查询
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //拷贝属性
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //保存套餐数据
        setmealMapper.insert(setmeal);
        //保存套餐菜品
        Long setmealId = setmeal.getId();
        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();
        //遍历菜品集合，为菜品设置套餐Id
        dishList.forEach(dish -> dish.setSetmealId(setmealId));
        //保存套餐菜品数据
        setmealDishMapper.insertBatch(dishList);
    }

    @Override
    public void delete(List<Long> ids) {
        //判断套餐是否在售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == 1) {
                //在售，不能删除
                throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除redis中缓存
        for(Long id : ids){
            //获取套餐catageoryId
            Long categoryId = setmealMapper.getById(id).getCategoryId();
            redisTemplate.delete("SetmealCache::" + categoryId);
        }
        //删除套餐菜品
        setmealDishMapper.deleteBySetmealId(ids);
        //删除套餐
        setmealMapper.delete(ids);
    }

    @Override
    public SetmealVO getById(Long id) {
        //调用Mapper接口
        Setmeal setmeal = setmealMapper.getById(id);
        //创建SetmealVO对象
        SetmealVO setmealVO = new SetmealVO();
        //拷贝属性
        BeanUtils.copyProperties(setmeal, setmealVO);
        //获取套餐的菜品
        Long setmealId = setmeal.getId();//套餐Id
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishBySetmealId(setmealId);
        //设置套餐的菜品
        setmealVO.setSetmealDishes(setmealDishes);
        //返回结果
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //创建套餐对象
        Setmeal setmeal = new Setmeal();
        //拷贝属性
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改套餐数据
        setmealMapper.update(setmeal);
        //获取套餐ID
        Long setmealId = setmealDTO.getId();
        //删除套餐菜品
        setmealDishMapper.deleteBySetmealId(Collections.singletonList(setmealId));
        //保存套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//获取套餐菜品
        //遍历菜品集合，为菜品设置套餐Id
        setmealDishes.forEach(dish -> dish.setSetmealId(setmealId));
        //保存套餐菜品数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //获取CategoryId
        Long categoryId = setmealMapper.getById(id).getCategoryId();
        //删除redis缓存
        redisTemplate.delete("SetmealCache::" + categoryId);
        //创建一个Setmeal对象
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //修改套餐状态
        setmealMapper.update(setmeal);

    }

    @Override
    public List<SetmealVO> list(Setmeal setmeal) {
        //获取setmeal对象
        List<Setmeal> setmealList = setmealMapper.list(setmeal);
        //创建SetmealVO对象
        List<SetmealVO> setmealVOList = new ArrayList<>();
        //遍历setmealList
        for (Setmeal s : setmealList) {
            SetmealVO setmealVO = new SetmealVO();
            //拷贝属性
            BeanUtils.copyProperties(s, setmealVO);

            //添加到集合中
            setmealVOList.add(setmealVO);;
        }
        return setmealVOList;
    }

    @Override
    public List<DishItemVO> getByIdWithDish(Long id) {
        //调用Mapper接口
        return setmealDishMapper.getByIdWithDish(id);
    }
}
